// 1 导入第三方模块
const gulp = require('gulp');
const cssmin = require('gulp-cssmin'); // 压缩css
const autoprefixer = require('gulp-autoprefixer'); // 给css的属性加前缀 
const htmlmin = require('gulp-htmlmin'); // 压缩html
const uglify = require('gulp-uglify'); // 压缩js
const babel = require('gulp-babel'); // es6转es5语法的
const del = require('del');// 删除文件的

// 2 书写任务
// 2-1 移动lib文件夹
const libHandler = ()=>{
    return gulp.src('./xiaomishpping/images/**')
    .pipe(gulp.dest('./dist/images'))
}
// 2-2 移动images文件夹
const imagesHandler = ()=>{
    return gulp.src('./xiaomishpping/img/**')
    .pipe(gulp.dest('./dist/img'))
}

// 2-3 压缩css文件
const cssHandler = ()=>{
    return gulp.src('./xiaomishpping/css/*.css')
    .pipe(autoprefixer())
    .pipe(cssmin())
    .pipe(gulp.dest('./dist/css'))
}

// 2-4 压缩html文件
const htmlHandler = ()=>{
    return gulp.src('./xiaomishpping/*.html')
    .pipe(htmlmin({
        collapseBooleanAttributes:true,
        collapseWhitespace:true,
        removeComments:true,
        removeAttributeQuotes:true,
        minifyJS:true,
        minifyCSS:true
    }))
    .pipe(gulp.dest('./dist'))

}

// 2-5 压缩js文件
const jsHandler = ()=>{
    return gulp.src('./xiaomishpping/js/**')
    .pipe(babel({
        presets:['@babel/env']
    }))
    .pipe(uglify())
    .pipe(gulp.dest('./dist/js'))
}

// 2-6 删除dist文件夹
const delHandler = ()=>{
    return del(['./dist'])
}


// 3 导出默认任务
module.exports.default = gulp.series(
    delHandler,
    gulp.parallel(cssHandler,jsHandler,htmlHandler,libHandler,imagesHandler),
)